<template>
  <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible" :close-on-click-modal="false">
    <el-form ref="dataForm" :rules="rules" :model="temp" label-position="left" label-width="80px" style="width: 80%; margin-left:50px;">
      <el-row :gutter="40">
        <el-col :span="24">
          <el-form-item align="center" label="标签" prop="label">
            <el-input v-model="temp.label" />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item align="center" label="标签规则" prop="whereType">
            <el-select v-model="temp.whereType" class="filter-item" placeholder="请选择标签规则" @change="choseRule">
              <el-option v-for="item in dictList('sql_where_type')" :key="'type' + item.label" :label="item.label" :value="item.value" />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="24" v-if="name != '是否为空' && name != '是否不为空'">
          <el-form-item align="center" label="标签值" prop="value">
            <el-input v-model="temp.value" />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item align="center" label="排序" prop="sort">
            <el-input v-model="temp.sort" />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item align="center" label="描述" prop="remarks">
            <el-input v-model="temp.remarks" />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="dialogFormVisible = false">
        {{ $t('table.cancel') }}
      </el-button>
      <el-button type="primary"  :loading="loading" @click="dialogStatus === 'create' ? createData() : updateData()">
        {{ $t('table.confirm') }}
      </el-button>
    </div>
  </el-dialog>
</template>

<script>
import { createMetadataLabel, updateMetadataLabel, getMetadataLabel } from '@/api/metadata/label/metadataLabel'

export default {
  name: 'MetadataLabelView',
  data() {
    return {
      rules: {
        label: [{ required: true, message: '标签为必填项', trigger: 'blur' }],
        whereType: [{ required: true, message: '标签规则为必填项', trigger: 'blur' }],
        value: [{ required: true, message: '标签值为必填项', trigger: 'blur' }],
        sort: [{ required: true, message: '排序为必填项', trigger: 'blur' }],
      },
      temp: {},
      textMap: {
        update: '编辑',
        create: '新建',
      },
      loading: false,
      dialogFormVisible: false,
      dialogStatus: '',
      name: '',
    }
  },
  methods: {
    choseRule(id) {
      this.dictList('sql_where_type').forEach((item) => {
        if (item.value == id) {
          this.name = item.label
        }
      })
      if (this.name == '是否为空' || this.name == '是否不为空') {
        this.rules.value[0].required = false
      } else {
        this.rules.value[0].required = true
      }
    },
    getList() {
      this.$emit('refreshList')
    },
    resetTemp() {
      this.loading = false
      this.temp = {
        id: undefined,
        gid: undefined,
        label: undefined,
        value: undefined,
        sort: undefined,
        remarks: undefined,
        createBy: undefined,
        createDate: undefined,
        updateBy: undefined,
        updateDate: undefined,
        delFlag: undefined,
        whereType: undefined,
      }
    },
    handleCreate(id) {
      this.resetTemp()
      this.temp.gid = id
      this.dialogStatus = 'create'
      this.name = ''
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    createData() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          this.loading = true
          createMetadataLabel(this.temp).then((response) => {
            this.loading = false
            if (response.data.code === 0) {
              this.getList()
              this.dialogFormVisible = false
              this.$message.success(response.data.msg)
            } else {
              this.$message.error(response.data.msg)
            }
          })
        }
      })
    },
    handleUpdate(id) {
      this.resetTemp()
      this.dialogStatus = 'update'
      this.name = ''
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
      getMetadataLabel(id).then((response) => {
        if (response.data.code === 0) {
          this.temp = response.data.data
          this.dictList('sql_where_type').forEach((item) => {
            if (item.value == this.temp.whereType) {
              this.name = item.label
            }
          })
          // if(this.name){
          //   this.temp.value=''
          // }
          if (this.name == '是否为空' || this.name == '是否不为空') {
            this.rules.value[0].required = false
          } else {
            this.rules.value[0].required = true
          }
        } else {
          this.dialogFormVisible = false
          this.$message.error(response.data.msg)
        }
      })
    },
    updateData() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          this.loading = true
          updateMetadataLabel(this.temp).then((response) => {
            this.loading = false
            if (response.data.code === 0) {
              this.dialogFormVisible = false
              this.getList()
              this.$message.success(response.data.msg)
            } else {
              this.$message.error(response.data.msg)
            }
          })
        }
      })
    },
  },
}
</script>
